﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-04-24 14:45:08
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class MsgShortMessage:IMsgShortMessage
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(id),0)+1
                               FROM [MsgShortMessage]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private MsgShortMessageInfo GetByReader(IDataReader dr)
         {
            MsgShortMessageInfo info = new MsgShortMessageInfo();
            object obj;
            obj=dr["id"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.id = (long)obj;
            }
            obj=dr["TypeName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.TypeName = obj.ToString();
            }
            obj=dr["Mobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Mobile = obj.ToString();
            }
            obj=dr["Contents"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Contents = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = ((obj)==DBNull.Value)?Convert.ToDateTime("1900-1-1"):Convert.ToDateTime(obj);
            }
            obj=dr["OpUserId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["OpUserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OpUserName = obj.ToString();
            }
            obj=dr["ResultMsg"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ResultMsg = obj.ToString();
            }
            obj=dr["Result"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Result = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["PlatformId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PlatformId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            return info;
         }
		public long Insert(MsgShortMessageInfo info)
		{
        
        
            string strSql=@"INSERT INTO MsgShortMessage			
			(			
			[TypeName],
			[Mobile],
			[Contents],
			[CreatedDate],
			[OpUserId],
			[OpUserName],
			[ResultMsg],
			[Result],
			[PlatformId]
			)
			 VALUES
			(			
			@TypeName,
			@Mobile,
			@Contents,
			@CreatedDate,
			@OpUserId,
			@OpUserName,
			@ResultMsg,
			@Result,
			@PlatformId
			)
			 SET @id=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@id",DbType.Int64, sizeof(Int64));  
            db.AddInParameter(dbc, "@TypeName",DbType.String, info.TypeName);
            db.AddInParameter(dbc, "@Mobile",DbType.String, info.Mobile);
            db.AddInParameter(dbc, "@Contents",DbType.String, info.Contents);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OpUserId",DbType.Int32, info.OpUserId);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ResultMsg",DbType.String, info.ResultMsg);
            db.AddInParameter(dbc, "@Result",DbType.Int32, info.Result);
            db.AddInParameter(dbc, "@PlatformId",DbType.Int32, info.PlatformId);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (long)db.GetParameterValue(dbc,"id");
         
		}
		
		
        public void Update(MsgShortMessageInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE MsgShortMessage SET ");
            sbSql.AppendLine("[TypeName] = @TypeName,");
                
            sbSql.AppendLine("[Mobile] = @Mobile,");
                
            sbSql.AppendLine("[Contents] = @Contents,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[OpUserId] = @OpUserId,");
                
            sbSql.AppendLine("[OpUserName] = @OpUserName,");
                
            sbSql.AppendLine("[ResultMsg] = @ResultMsg,");
                
            sbSql.AppendLine("[Result] = @Result,");
                
            sbSql.AppendLine("[PlatformId] = @PlatformId");
                
            sbSql.Append(" where  [id] = @id");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@id",DbType.Int64, info.id);
            db.AddInParameter(dbc, "@TypeName",DbType.String, info.TypeName);
            db.AddInParameter(dbc, "@Mobile",DbType.String, info.Mobile);
            db.AddInParameter(dbc, "@Contents",DbType.String, info.Contents);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OpUserId",DbType.Int32, info.OpUserId);
            db.AddInParameter(dbc, "@OpUserName",DbType.String, info.OpUserName);
            db.AddInParameter(dbc, "@ResultMsg",DbType.String, info.ResultMsg);
            db.AddInParameter(dbc, "@Result",DbType.Int32, info.Result);
            db.AddInParameter(dbc, "@PlatformId",DbType.Int32, info.PlatformId);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(long id)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [MsgShortMessage]  ");
            strSql.Append("WHERE [id]=@id ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@id",DbType.Int64, id);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<MsgShortMessageInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<MsgShortMessageInfo> list = new List<MsgShortMessageInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "MsgShortMessage");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    MsgShortMessageInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public MsgShortMessageInfo GetByID(long id)
		{
            Model.MsgShortMessageInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [MsgShortMessage] ");
            strSql.Append("WHERE [id]=@id ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@id",DbType.Int64, id);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}